package com.cnc.servlet;

import com.cnc.service.CourseService;
import com.cnc.service.Impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.Collection;
import java.util.UUID;

@MultipartConfig
@WebServlet("/course")
public class CourseServlet extends BaseServlet {

    CourseService cs = new CourseServiceImpl();

    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");
        String type = req.getParameter("type");
        PrintWriter writer = resp.getWriter();
        writer.print(cs.getCourseList(currentPage, pageSize, search,type));
    }

    protected void getCourseInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        if(cid!=null) {
            resp.getWriter().print(cs.getCourseInfo(cid));
        }
    }

    protected void upload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part file = req.getPart("file");
        String fileName = file.getSubmittedFileName();
        String value = req.getParameter("value");
        if (value.equals("1")) {
            fileName = upLoad(file, "D:/cs_res/course/pic/", fileName);
        } else {
            fileName = upLoad(file, "D:/cs_res/course/video/", fileName);
        }
        resp.getWriter().print(fileName);
    }

    protected void remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InterruptedException {
        String fileName = req.getParameter("filename");
        String value = req.getParameter("value");
        if (value.equals("1")) {
            new File("D:/cs_res/course/pic/" + fileName).delete();

        } else {
            new File("D:/cs_res/course/video/" + fileName);
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if (data != null) {
            resp.getWriter().print(cs.updateCourse(data));
        }
        //resp.getWriter().print(us.updateUser(req.getReader().readLine()));
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if (data != null) {
            resp.getWriter().print(cs.addCourse(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    private String upLoad(Part file, String path, String fileName) throws IOException {
        String suffix = fileName.substring(fileName.indexOf('.'));
        String url = path + fileName;
        while (new File(url).exists()) {
            fileName = UUID.randomUUID() + suffix;
            url = path + fileName;
        }
        file.write(url);
        return fileName;
    }

    protected void delCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if (data != null) {
            resp.getWriter().print(cs.delAllSelect(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void allCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print(cs.getAllCourse());
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void getCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String size = req.getParameter("size");
        String search = req.getParameter("search");
        String type = req.getParameter("type");
        PrintWriter writer = resp.getWriter();
        writer.print(cs.getList(currentPage, size, search,type));
    }
}
